#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

struct emp{
  int id;
  char name[20];
  double sal;
};

int writeemp()
{
  int fd = open("emp.dat",
    O_RDWR|O_CREAT|O_TRUNC,0666);
  if(fd==-1) perror("open"),exit(-1);
//  struct emp em = {1,"zhangfei",12000.0};
  struct emp em ;
  char name[20];
  printf("请输入用户ID：");
			scanf("%d",&em.id);
			scanf( "%*[^\n]");
	        scanf("%*c");
printf( "请输入用户姓名 : " );
		        fgets(name, sizeof(name), stdin);
		        if (strlen(name) == sizeof (name) - 1 && name[sizeof(name) - 2] != '\n')
		        {
		          	scanf( "%*[^\n]");
		            scanf( "%*c");
		        }
		        strcpy(em.name , name);
		        printf("请输入用户薪资：");
				scanf("%lf",&em.sal);
				scanf( "%*[^\n]");
			    scanf("%*c");	        
  int res = write(fd,&em,sizeof(em));
  if(res != -1) printf("write ok\n");
  else printf("write error\n");

  close(fd);
}

int reademp()
{
  int fd = open("emp.dat",O_RDWR);
  if(fd==-1) perror("open"),exit(-1);
  struct emp em;
  int res = read(fd,&em,sizeof(em));
  if(res != -1) {
     printf("%d,%s,%lf\n",em.id,em.name,
      em.sal); }
  else printf("read error\n");
  close(fd);
}

void main()
{
  writeemp();
  reademp();  

}
